
<template>
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="1100px"
    center
    :close-on-click-modal="false"
  >
    <el-form
      ref="form"
      :rules="rules"
      style="padding-top: 10px"
      :model="form"
      label-width="100px"
    >
      <el-row>
        <el-col :span="7">
          <el-form-item label="客商名称" prop="supName" label-width="90px">
            <el-input
              v-model="form.supName"
              readonly
              :disabled="disableForm || optType !== 'add'"
              @click.native="
                optType === 'add' ? (showSupplierListModal = true) : null
              "
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-circle-plus-outline"
              />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item
            label="统一社会信用代码"
            prop="supUnifiedSocialCreditCode"
            label-width="150px"
          >
            <el-input v-model="form.supUnifiedSocialCreditCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item
            label="身份证"
            prop="supIdentifyNumber"
            label-width="90px"
          >
            <el-input v-model="form.supIdentifyNumber" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="7">
          <el-form-item label="客商编号" prop="supNumber" label-width="90px">
            <el-input v-model="form.supNumber" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="考核编号" prop="basNumber" label-width="150px">
            <el-input
              v-model="form.basNumber"
              :disabled="true"
              placeholder="自动生成"
            />
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="评价分值" prop="basFraction" label-width="90px">
            <el-input v-model="form.basFraction" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="7">
          <el-form-item label="评价等级" prop="basRating" label-width="90px">
            <el-select
              v-model="form.basRating"
              :disabled="disableForm"
              style="width: 100%"
              placeholder="请选择"
            >
              <el-option
                v-for="item in ratingLevels"
                :key="item.name"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item
            label="评价日期区间"
            prop="basRangeTime"
            label-width="150px"
          >
            <el-date-picker v-model="form.basRangeTime" type="month" format="yyyy-MM" value-format="yyyy-MM" style="width:290px" placeholder="请选择年月" />
            <!-- <el-input v-model="form.basRangeTime" :disabled="disableForm" /> -->
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="备注" prop="basRemark" label-width="90px">
            <el-input v-model="form.basRemark" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="综合评价" prop="basDescription" label-width="90px">
        <el-input v-model="form.basDescription" :disabled="disableForm" />
      </el-form-item>
      <!-- <el-form-item label="">
        <div style="display: flex;align-items: center;">
          <el-button style="margin-right:10px;margin-top:2px;" size="small" type="primary" @click="showTreeModal = true">{{ typeLabel }}</el-button>
          <uploader v-model="fileList" :disabled="disableForm" :can-download="optType==='detail'" />
        </div>
      </el-form-item> -->
    </el-form>
    <div style="padding: 0 10px 0 26px">
      <div style="padding-bottom: 10px">
        <el-button
          type="primary"
          @click="showTargetSetListModal = true"
        >引入考核指标</el-button>
        <el-button type="primary" @click="deleteRow">删除考核指标</el-button>
      </div>
      <com-table
        v-model="selectRow"
        highlight-current-row
        :data="targetList"
        :columns="columns"
        :no-page="true"
      >
        <template v-slot:basFraction="{ row }">
          <!-- 获取评定分值 -->
          <el-input v-model="row.basFraction" @input="changeInput" />
        </template>
        <template v-slot:basRemark="{ row }">
          <el-input v-model="row.basRemark" :disabled="disableForm" />
        </template>
        <!-- <template v-slot:options="{row}">
        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editBuildUnit(row)">编辑</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="delBuildUnit(row)">删除</el-button>
      </template> -->
      </com-table>
      <el-button
        style="margin-top: 10px"
        type="primary"
        @click="showFileModal = true"
      >附件资料({{ fileList.length }})</el-button>
      <file-uploader
        v-model="fileList"
        :show-modal.sync="showFileModal"
        :disabled="disableForm"
        :can-download="optType === 'detail'"
      />
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveSupplier">确 定</el-button>
    </span>
    <supplier-select-list
      :type="type"
      :show-modal.sync="showSupplierListModal"
      @getSelectedData="selectSupplier"
    />
    <target-set-select-list
      :type="type"
      :show-modal.sync="showTargetSetListModal"
      @getSelectedRows="getSelectedRows"
    />
  </el-dialog>
</template>

<script>
import {
  addSupplierAssessInfo,
  updateSupplierAssessInfo,
  getSupplierAssessInfoDetail
} from '@/api/travellingTraderManage'
import { getAreaList } from '@/api/common'
import {
  identifyTypes,
  organizationLevels,
  ratingLevels
} from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
import supplierSelectList from './supplierSelectList.vue'
import targetSetSelectList from './targetSetSelectList.vue'
// import supplierTree from './supplierTree.vue'
export default {
  name: 'AddJudge',
  components: {
    fileUploader,
    targetSetSelectList,
    supplierSelectList
  },
  props: {
    title: {
      type: String,
      default: ''
    },
    typeLabel: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: ''
    },
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      selectRow: null,
      targetList: [],
      columns: [
        {
          label: '编号',
          prop: 'idxNumber'
        },
        {
          label: '名称',
          prop: 'idxName'
        },
        {
          label: '评定指标',
          prop: 'idxDescription'
        },
        {
          label: '指标分值',
          prop: 'idxFraction'
        },
        {
          label: '评定分值',
          // prop: 'fraction',
          slot: 'basFraction'
        },
        {
          label: '备注',
          // prop: 'fraction',
          slot: 'basRemark'
        }
      ],
      showSupplierListModal: false,
      showTargetSetListModal: false,
      options: [],
      ratingLevels,
      identifyTypes,
      organizationLevels,
      addType: '', // same   under
      visible: false,
      supplierInfo: {},
      form: {
        supName: '',
        supNumber: '',
        supUnifiedSocialCreditCode: '',
        supIdentifyNumber: '',
        basFraction: '',
        basNumber: '',
        basDescription: '',
        basRangeTime: '',
        basRemark: '',
        basRating: ''
      },
      fileList: [],
      rules: {
        supName: [
          { required: true, message: '请选择客商名称', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    },
    typeTitle() {
      const titles = ['分包队伍', '材料供应商', '设备供应商', '其他供应商']
      return this.type ? titles[this.type - 1] : ''
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getDocDetail(this.data.saiId)
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
        this.targetList = []
      }
    }
  },
  mounted() {},
  methods: {
    deleteRow() {
      if (this.selectRow) {
        const index = this.targetList.indexOf(this.selectRow)
        this.targetList.splice(index, 1)
      } else {
        this.$message({
          type: 'success',
          message: '请选中删除行!'
        })
      }
    },
    getSelectedRows(data) {
      this.targetList = this.targetList.concat(
        data.map((item) => {
          return {
            ...item,
            idxNumber: item.number,
            idxName: item.name,
            idxDescription: item.description,
            idxFraction: item.fraction,
            basFraction: '',
            basRemark: ''
          }
        })
      )
    },
    selectSupplier(data) {
      if (data) {
        this.supplierInfo = data
        const { name, number, unifiedSocialCreditCode, identifyNumber } = data
        this.form.supName = name
        this.form.supNumber = number
        this.form.supUnifiedSocialCreditCode = unifiedSocialCreditCode
        this.form.supIdentifyNumber = identifyNumber
      }
    },
    getDocDetail(id) {
      getSupplierAssessInfoDetail({ supplierAssessInfoId: id }).then((res) => {
        const { lstAttachment, lstSupplierAssessIndex, supplierAssessInfo } =
          res.data || {}
        if (supplierAssessInfo) {
          Object.keys(this.form).forEach((key) => {
            this.form[key] = supplierAssessInfo[key]
          })
          this.supplierInfo.id = supplierAssessInfo.supplierInfoId
        }
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        if (lstSupplierAssessIndex && lstSupplierAssessIndex.length > 0) {
          this.targetList = lstSupplierAssessIndex.map((item) => {
            return {
              ...item
            }
          })
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then((res) => {
        this.provinceData = res.data || []
      })
    },
    saveSupplier() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      if (!this.supplierInfo.id) {
        this.$message({
          type: 'error',
          message: `请选择${this.typeTitle}`
        })
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          // const formData = JSON.parse(JSON.stringify(
          //   this.form
          // ))
          // const {
          //   fraction,
          //   number,
          //   description,
          //   rangeTime,
          //   remark,
          //   rating
          // } = formData
          const params = {
            supplierAssessInfo: {
              ...this.form,
              supplierInfoId: this.supplierInfo.id,
              basType: this.type,
              ...(this.optType === 'edit'
                ? {
                  id: this.data.saiId
                }
                : {})
            },
            ...(this.targetList.length
              ? {
                lstSupplierAssessIndex: this.targetList.map((item) => {
                  return {
                    ...item,
                    id: null,
                    ...(this.optType === 'edit'
                      ? {
                        id: item.id,
                        supplierAssessInfoId: this.data.saiId,
                        supplierIndexInfoId: item.supplierIndexInfoId
                      }
                      : { supplierIndexInfoId: item.id })
                  }
                })
              }
              : { lstSupplierAssessIndex: [] }),
            lstAttachment: this.fileList
          }
          const fn =
            this.optType === 'add'
              ? addSupplierAssessInfo
              : updateSupplierAssessInfo
          fn(params).then((res) => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              // this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    },
    // 获取输入框分值
    changeInput(e) {
      let sum = 0
      this.targetList.forEach(item => {
        if (item.basFraction) {
          sum += Number.parseFloat(item.basFraction)
        }
      })
      this.form.basFraction = sum.toFixed(2)
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title {
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409eff;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>

